/*****************************************************************************
*
*  PROJECT:     SlipStream
*               (CSoundManager Class)
*  LICENSE:     GNU General Public License v3 - http://www.gnu.org/licenses/gpl.html

*  DEVELOPERS:  Philip Farquharson 	<B00137077@studentmail.uws.ac.uk>
*				John Gray 			<B00145524@studentmail.uws.ac.uk>		
*				Zohaib Bhatti		<B00104280@studentmail.uws.ac.uk>
*				Euan Hislop 		<B00138636@studentmail.uws.ac.uk>
*
*****************************************************************************/
#include "CSoundManager.h"

template<> CSoundManager * CSingleton< CSoundManager >::m_pSingleton = NULL;

CSoundManager::CSoundManager( void )
{
	m_pEngine = createIrrKlangDevice();
}

CSoundManager::~CSoundManager( void )
{
	std::list<CSound2D*>::const_iterator iter = sound2ds.begin();
	for ( ; iter != sound2ds.end (); ++iter )
	{
		CSound2D* pSound2D = static_cast < CSound2D* > ( *iter );
	
		assert ( pSound2D );
	
		// free our resources
		SAFE_DELETE( pSound2D );
	}

	std::list<CSound3D*>::const_iterator iter3d = sound3ds.begin();
	for ( ; iter3d != sound3ds.end (); ++iter3d )
	{
		CSound3D* pSound3D = static_cast < CSound3D* > ( *iter3d );

		assert ( pSound3D );

		// free our resources
		SAFE_DELETE( pSound3D );
	}
}

CSound2D* CSoundManager::createSound ( const char* szFilename, bool bPlay, bool bLoop, float fVolume )
{
	ISound* SoundInterface = m_pEngine->play2D (szFilename, bLoop, !bPlay, false, ESM_AUTO_DETECT, true);
	SoundInterface->setVolume(fVolume);

	CSound2D* pSound2D = new CSound2D ( SoundInterface );
	sound2ds.push_back( pSound2D );
	return pSound2D;
}

CSound3D* CSoundManager::createSound ( const char* szFilename, bool bPlay, bool bLoop, float fVolume, CVector3 vecPosition, float fMinDistance, float fMaxDistance )
{
	vec3df v(vecPosition.getX(), vecPosition.getY(), vecPosition.getZ());

	ISound* SoundInterface = m_pEngine->play3D (szFilename, v, bLoop, !bPlay, false, ESM_AUTO_DETECT, true);
	SoundInterface->setVolume(fVolume);
	SoundInterface->setMinDistance(fMinDistance);
	SoundInterface->setMaxDistance(fMaxDistance);

	CSound3D* pSound3D = new CSound3D ( SoundInterface );
	sound3ds.push_back( pSound3D );
	return pSound3D;
}

void CSoundManager::destroySound ( CSound2D* pSound )
{
	assert ( pSound );

	sound2ds.remove( pSound );

	SAFE_DELETE(pSound);
}

void CSoundManager::destroySound ( CSound3D* pSound )
{
	assert ( pSound );

	sound3ds.remove( pSound );

	SAFE_DELETE(pSound);
}

void CSoundManager::updateCameraPosition ( CVector3 vecPosition, CVector3 vecLookAt, CVector3 vecVelocity )
{
	vec3df position ( vecPosition.getX(), vecPosition.getY(),vecPosition.getZ() );
	vec3df lookat ( vecLookAt.getX(), vecLookAt.getY(), vecLookAt.getZ() );
	vec3df velocity ( vecVelocity.getX(), vecVelocity.getY(),vecVelocity.getZ() );
	vec3df upvector ( 0, 1, 0 );

	m_pEngine->setListenerPosition ( position, lookat, velocity, upvector );
}